package com.example.anew.journey.mynewjourney.firstchangelogic.FirstChangeDevices;

import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.anew.journey.mynewjourney.R;
import com.example.anew.journey.mynewjourney.base.FirstChangeAllBaseActivity;
import com.example.anew.journey.mynewjourney.bean.ISSuccessMain;
import com.example.anew.journey.mynewjourney.bean.TakeDateBean;
import com.example.anew.journey.mynewjourney.bean.eventNotification;
import com.example.anew.journey.mynewjourney.customview.MyRecyclerView;
import com.example.anew.journey.mynewjourney.firstchangelogic.FirstChangeDevices.persenter.FirstChangeDevicesPersenter;
import com.example.anew.journey.mynewjourney.firstchangelogic.FirstChangeDevices.view.ISFirstChangeDevicesView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.Bind;
import butterknife.ButterKnife;

import static butterknife.ButterKnife.bind;

/**
 * 杭州惟君医疗科技有限公司14
 * Created by 20433 on 2018/3/15
 */

public class FirstChangeDevicesActivity extends FirstChangeAllBaseActivity implements ISFirstChangeDevicesView,View.OnClickListener{
    @Bind(R.id.no_date_picture)
    ImageView noDatePicture;
    @Bind(R.id.activity_first_change_devices_my_recyclerview)
    MyRecyclerView activityFirstChangeDevicesMyRecyclerview;
    private View view;

    private FirstChangeDevicesPersenter mFirstChangeDevicesPersenter = new FirstChangeDevicesPersenter(this);
    private PopupWindow mPopWindowRemoveBinding;
    private TextView tv_title;
    private TextView tv_prompt_desc;
    private TextView tv_i_think;
    private TextView tv_confirm_unbundling;
    private String devices_id;
    private int myType;
    private int device_page_identification;
    private RelativeLayout rl_error_pager;

    /**
     * @desc 主要内容的布局资源填充进来
     */
    @Override
    protected View setMainContentView() {
        view = View.inflate(this, R.layout.activity_first_change_devices, null);
        bind(this, view);
        return view;
    }

    /**
     * @desc 初始化数据 onCreate()
     */
    @Override
    protected void initView(View view) {
        displayToolbar();
        displayReturn();
        displayTvName();
        displayIvShared();

        setToolbarBg(0xff2196F3);
        setReturnrePictuResources(R.mipmap.return_02);
        setTvNameEachAttribute(20,0xffffffff,"设备");
        setIvSharedPictuResources(R.mipmap.search_bt);

        rl_error_pager = view.findViewById(R.id.activity_first_change_devices_rl_error_pager);
        EventBus.getDefault().register(this);
        
        initDate();
        
    }

    private void initDate() {
        device_page_identification = getIntent().getIntExtra("device_page_identification",0);
        mFirstChangeDevicesPersenter.initDevicesList(activityFirstChangeDevicesMyRecyclerview,this, device_page_identification, rl_error_pager);

    }

    /**
     * @desc Activity 销毁 释放资源
     */
    @Override
    protected void releaseResources() {
        ButterKnife.unbind(view);
        EventBus.getDefault().unregister(this);
    }

    /**
     * @desc 网络监听 用于提示用户有网络了
     */
    @Override
    protected void promptHaveInternet() {

    }

    /**
     * @desc 网络监听 用于提示用户没有网络了
     */
    @Override
    protected void promptNoInternet() {

    }

    /**
     * @desc 点击分享按钮
     */
    @Override
    protected void onClickShared() {
        Toast.makeText(this,"敬请期待",Toast.LENGTH_SHORT).show();
//        Intent intent =  new Intent(FirstChangeDevicesActivity.this, FirstChangeSearchActivity.class);
//        startActivity(intent);
    }

    /**
     * @desc 点击发布按钮
     */
    @Override
    protected void onClickRelease() {

    }

    /**
     * @desc 点击返回按钮
     */
    @Override
    protected void onClickReturn() {
        finish();
    }

    // 跳转到设备测试页面
    @Override
    public void toDevicesTestPagerActivity() {

    }
    // 跳转到绑定页面
    @Override
    public void toBandingPagerActivity() {

    }
    // 显示提示Toast信息
    @Override
    public void showToastInformation(String str) {

    }
    // 显示进度
    @Override
    public void displayProgress() {

    }
    // 隐藏进度
    @Override
    public void hiddenProgress() {

    }

    @Override
    public void showRemoveBinding(String id,int type) {
        View contentView = LayoutInflater.from(this).inflate(R.layout.first_change_add_devices_remove_banding, null);
        mPopWindowRemoveBinding = new PopupWindow(contentView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
        mPopWindowRemoveBinding.setContentView(contentView);
        devices_id = id;
        myType = type;
        findRemoveBinding(contentView);
        //显示PopupWindow
        View rootview = LayoutInflater.from(this).inflate(R.layout.activity_add_devices, null);
        mPopWindowRemoveBinding.showAtLocation(rootview, Gravity.CENTER, 0, 0);
    }

    @Override
    public void removeBindingIThink() {
        mPopWindowRemoveBinding.dismiss();
    }

    private void findRemoveBinding(View contentView) {

        tv_title = (TextView)contentView.findViewById(R.id.first_change_add_devices_remove_banding_tv_title);
        tv_prompt_desc = (TextView)contentView.findViewById(R.id.first_change_add_devices_remove_banding_tv_prompt_desc);
        tv_i_think = (TextView)contentView.findViewById(R.id.first_change_add_devices_remove_banding_tv_i_think);
        tv_confirm_unbundling = (TextView)contentView.findViewById(R.id.first_change_add_devices_remove_banding_tv_confirm_unbundling);


        tv_i_think.setOnClickListener(this);
        tv_confirm_unbundling.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.first_change_add_devices_remove_banding_tv_i_think:    //认绑定失败 我在想想
                mFirstChangeDevicesPersenter.verifyBindingFailedIThink();
                break;
            case R.id.first_change_add_devices_remove_banding_tv_confirm_unbundling:  //认绑定失败 确认解绑
                mFirstChangeDevicesPersenter.verifyBindingFailedConfirmUnbundling(devices_id,this,myType);
                break;
        }

    }

    //订阅方法，当接收到事件的时候，会调用该方法
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(eventNotification event){
        if(event.getDeviePagerID() == device_page_identification ){
            TakeDateBean bean =  mFirstChangeDevicesPersenter.getMyuserID(this);
            mFirstChangeDevicesPersenter.loadMainDevicesDate(activityFirstChangeDevicesMyRecyclerview, this,rl_error_pager);
        }

    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMEvent(ISSuccessMain event){
        if(event.isAtMain()){
            mFirstChangeDevicesPersenter.loadMainDevicesDate(activityFirstChangeDevicesMyRecyclerview, this,rl_error_pager);
        }

    }
}
